<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yak\layui\widgets\form;

use Yii;
use yii\base\InvalidCallException;
use yii\base\Model;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\web\View;

/**
 * ActiveForm is a widget that builds an interactive HTML form for one or multiple data models.
 *
 * For more details and usage information on ActiveForm, see the [guide article on forms](guide:input-forms).
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class ActiveForm extends \yii\widgets\ActiveForm
{
    /**
     * @var array
     */
    public $options = [
        'class' => 'layui-form layui-form-pane',
        'lay-filter' => '_FORM'
    ];

    /**
     * @var string
     */
    public $fieldClass = 'yak\layui\widgets\form\ActiveField';

    /**
     * @var bool
     */
    public $enableAjaxValidation = false;

    /**
     * @var bool
     */
    public $enableClientValidation = false;

    /**
     * @var array
     */
    public $ajaxOptions = [
        'formName' => 'Create-Class',
        'modelClass' => ''
    ];

    /**
     * This registers the necessary JavaScript code.
     * @since 2.0.12
     *
     *
     * layui.use(['form', 'ax', 'helper'], function () {
     *
     * var config = {$config}
     * , form = layui.form
     * , helper = layui.helper
     * , ax = layui.ax;
     *
     * form.on('submit(' + config.filter + ')', function(data) {
     *
     * var ajax = new ax(config.url, function (res) {
     * var index = parent.layer.getFrameIndex(window.name);
     * parent.layer.close(index);
     * });
     * ajax.setMethod(config.method);
     * ajax.setDataCondition(config.condition);
     * var field = data.field;
     * var _csrf = helper.removeValue(field, '_csrf', '');
     * ajax.setDataValue('_csrf', _csrf);
     * ajax.setDataData(data.field);
     * ajax.setHeaders(config.headers);
     * ajax.send();
     * return false;
     * })
     *
     * })
     *
     *
     *
     */
    public function registerClientScript()
    {
        $action = Url::to($this->action);
        $condition = ArrayHelper::remove($this->ajaxOptions, 'condition', []);
        $ajaxConfig = [
            'url' => $action,
            'method' => $this->method,
            'filter' => ArrayHelper::getValue($this->ajaxOptions, 'submitEvent', '_SUBMIT_FORM'),
            'condition' => $condition,
            'headers' => [
                'AOP' => 1,
                'Model-Class' => $this->ajaxOptions['modelClass'],
                'Model-Method' => ArrayHelper::getValue($this->ajaxOptions, 'modelMethod', 'save')
            ],
        ];
        $config = json_encode($ajaxConfig);
        $js = "layui.use(['form','ax','helper'],function(){var config={$config},form=layui.form,helper=layui.helper,ax=layui.ax;form.on('submit('+config.filter+')',function(data){var ajax=new ax(config.url,function(res){var index=parent.layer.getFrameIndex(window.name);parent.layer.close(index)});ajax.setMethod(config.method);ajax.setDataCondition(config.condition);var csrf=helper.removeValue(data.field,'_csrf','');ajax.setDataValue('_csrf',csrf);ajax.setDataData(data.field);ajax.setHeaders(config.headers);ajax.send();return false;})})";
        $this->getView()->registerJs($js, View::POS_END);
    }

}
